package com.wms.util;

import com.wms.entity.WmsStorageBatch;
import com.wms.entity.WmsStorageInOut;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.openbravo.dal.service.OBDal;
import org.openbravo.model.common.enterprise.Locator;
import org.openbravo.model.common.enterprise.Warehouse;
import org.openbravo.model.common.uom.UOM;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class WmsStorageUtil {
	/**
	 * 根据物料批次号与物料编码获取库存批次信息
	 * @param batchNo
	 * @param productId
	 * @return
	 */
	public static WmsStorageBatch getWmsStorageBatch(String batchNo, String productId) {
		String hql = "from " + WmsStorageBatch.ENTITY_NAME + " e where e.batchNo = ?";
		if (productId != null && !"".equals(productId)) {
			hql += " and e.product.id = ?";
		}
		Query query = OBDal.getInstance().getSession().createQuery(hql);
		query.setString(0, batchNo);
		if (productId != null && !"".equals(productId)) {
			query.setString(1, productId);
		}
		return (WmsStorageBatch)query.uniqueResult();
	}
	/**
	 * 占用库存
	 */
	public synchronized static void addTakeupStorage(WmsStorageBatch storageBatch, BigDecimal qty, Warehouse warehouse,
													 Locator locator, String dataId, String code, String optMode, UOM uom, String outType, String otherType) {
		WmsStorageInOut takeup = new WmsStorageInOut();
		takeup.setWmsStorageBatch(storageBatch);
		takeup.setWarehouse(warehouse);
		takeup.setLocator(locator);
		takeup.setProduct(storageBatch.getProduct());
		takeup.setQty(qty.doubleValue() < 0 ? qty : new BigDecimal(qty.doubleValue() * -1));
		takeup.setUom(uom);
		takeup.setOptMode(optMode);
		takeup.setOptCode(code);
		takeup.setData(dataId);
		takeup.setStatus("LOCKED");
		takeup.setSelf(storageBatch.isSelf());
		
		OBDal.getInstance().save(takeup);
	}
	
	/**
	 * 出库
	 */
	public synchronized static void addOutStorage(WmsStorageBatch storageBatch, BigDecimal qty, Warehouse warehouse,
												  Locator locator, String dataId, String code, String optMode, UOM uom, String outType, String otherType) {
		WmsStorageInOut out = new WmsStorageInOut();
		out.setWmsStorageBatch(storageBatch);
		out.setWarehouse(warehouse);
		out.setLocator(locator);
		out.setProduct(storageBatch.getProduct());
		out.setQty(qty.doubleValue() < 0 ? qty : new BigDecimal(qty.doubleValue() * -1));
		out.setUom(uom);
		out.setOptMode(optMode);
		out.setOptCode(code);
		out.setData(dataId);
		out.setStatus("OK");
		out.setSelf(storageBatch.isSelf());
		
		out.setMainType(outType);//其他出库方式
		out.setSubType(otherType);
		
		OBDal.getInstance().save(out);
	}
	
	/**
	 * 入库
	 */
	public synchronized static void addInStorage(WmsStorageBatch storageBatch, BigDecimal qty,
												 Warehouse warehouse, Locator locator, String dataId, String code, String optMode, UOM uom, String outType, String otherType) {
		WmsStorageInOut in = new WmsStorageInOut();
		in.setWmsStorageBatch(storageBatch);
		in.setWarehouse(warehouse);
		in.setLocator(locator);
		in.setProduct(storageBatch.getProduct());
		in.setQty(qty.doubleValue() < 0 ? new BigDecimal(qty.doubleValue() * -1) : qty);
		in.setUom(uom);
		in.setOptMode(optMode);
		in.setOptCode(code);
		in.setData(dataId);
		in.setStatus("OK");
		in.setSelf(storageBatch.isSelf());
		
		OBDal.getInstance().save(in);
	}
	
	/**
	 * 预投出库记录次数
	 * @param batchNo 批次
	 * @param product 物料编码
	 * @param vendor 供应商编码
	 * @param locator 库位编码
	 * @param warehouse 仓库编码
	 * @param gradeId 品质级别ID
	 */
	public synchronized static int findInStorage(String batchNo, String product, String vendor, String locator, String warehouse, String gradeId) {
		String storageSql = "select wms_storage_in_out_id			"+
			"  from wms_storage_batch sb, wms_storage_in_out io			"+
			" where sb.wms_storage_batch_id = io.wms_storage_batch_id	and io.qty < 0		";
			if (batchNo != "" && !"".equals(batchNo)) {
				storageSql += "   and sb.batch_no = '"+batchNo+"'			";
			}
			if (product != "" && !"".equals(product)) {
				storageSql += "   and sb.m_product_id = '"+product+"'			";
			}
			if (vendor != "" && !"".equals(vendor)) {
				storageSql += "   and sb.emes_vendor_id = '"+vendor+"'			";
			}
			if (locator != "" && !"".equals(locator)) {
				storageSql += "   and io.m_locator_id = '"+locator+"'			";
			}
			if (warehouse != "" && !"".equals(warehouse)) {
				storageSql += "   and m_warehouse_id = '"+warehouse+"'			";
			}
			if (gradeId !="" && !"".equals(gradeId)) {
				storageSql += "   and Wms_Quality_Grade_ID = '"+gradeId+"'			";
			}
		Query query = OBDal.getInstance().getSession().createSQLQuery(storageSql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		List<Map<String, Object>> list =query.list();
		return list.size();
	}
}